package model

import (
	"errors"
	"server/common"
)

type Jenkins struct {
	ID      int64  `gorm:"column:id"`
	Name    string `gorm:"column:name;type:varchar(32);unqiue"`
	Status  bool   `gorm:"column:id;type:bool;"`
	Product int64  `gorm:"column:id;not null;type:varchar(100)"`
}

type JenkinsApi struct {
	JID  uint   `json:"jid"`
	JOBS string `json:"jobs"`
	JURL string `json:"jurl"`
}

func (j *Jenkins) Update() error {
	js := []Jenkins{}
	if err := common.DB.Where("name = ?", j.Name).First(&js).Error; err != nil {
		return err
	}
	if len(js) == 0 {

	}
	return nil
}

func (j *Jenkins) Create() error {
	if err := common.DB.Create(&j).Error; err != nil {
		return err
	}
	return nil
}

func QueryIdleJob(p string) ([]Jenkins, error) {
	jobs := []Jenkins{}
	if err := common.DB.Where("status = ? and product = ?", false, p).Find(&jobs).Error; err != nil {
		return jobs, err
	}
	if len(jobs) == 0 {
		return nil, errors.New(p + "：未获取到空闲job")
	}
	return jobs, nil
}
